//处理参数对象
const setParams = params => {
    let queryArr = []
    for (let key in params) {
      let str = key + '=' + params[key]
      queryArr.push(str)
    }
    return queryArr.join('&')
  }
  
  const harryAjax = option => {
    //ajax函数返回一个Promise对象
    return new Promise((res, rej) => {
      //创建xhr实例
      const xhr = new XMLHttpRequest()
      //处理参数格式
      const query = setParams(option.data)
  
      //设置响应格式
      xhr.responseType = 'json'
  
      //判断请求方式
      if (option.method.toUpperCase() === 'GET') {
        xhr.open('GET', query ? option.url + '?' + query : option.url)
        xhr.send()
      } else if (option.method.toUpperCase() === 'POST') {
        //设置请求头
  
        xhr.open('POST', option.url)
        xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
        xhr.send(query)
      }
  
      //监听
      xhr.onreadystatechange = function () {
        if (xhr.readyState === 4) {
          if (xhr.status === 200) {
            res(xhr.response)
          } else {
            rej(xhr.status)
          }
        }
      }
    })
  }
  